﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;

namespace EasyPayDemo.WeiPay
{
    /// <summary>
    /// jsapi 下单 
    /// </summary>
    public class ExcuteJsapi : IFunction
    {
        public override void Execute()
        {

            M_JsApi request = new M_JsApi();

            request.orgId = WeiPayConfig.orgId;
            request.orgMercode = WeiPayConfig.orgMercode;
            request.orgTermno = WeiPayConfig.orgTermno;
            request.signType = WeiPayConfig.SIGN_TYPE_RSA256;

            request.orgTrace = WeiPayUtils.GetOrgTrace(WeiPayConfig.orgId);

            // data数据 请求时 需签名
            M_JsApi.Data dataBean = new M_JsApi.Data();
            dataBean.orgBackUrl = "https://t-wapi.bhecard.com:1443/selfNotice/backNotice";
            dataBean.tradeCode = "WAJS1";
            dataBean.payerId = "2088102936140877";
            dataBean.orderInfo = "测试主扫支付10分";
            dataBean.tradeAmt = 10;
            dataBean.timeoutMinutes = 300;

            //string key = WeiPayUtils.GetRsaKey();
            M_JsApi.Appenddata appenddata = new M_JsApi.Appenddata();

            M_JsApi.Identity identity = new M_JsApi.Identity();

            string AesKey = WeiPayUtils.GetAesKey();

            dynamic encryInfo = new ExpandoObject();
            encryInfo.mobile = "18579106328";
            encryInfo.name = "潘文敏";
            encryInfo.number = "362228198712100518";
            identity.encryptInfo= EncryptionUtil.AesEncrypt(JsonConvert.SerializeObject(encryInfo), AesKey);
            Console.WriteLine(AesKey);
            Console.WriteLine(JsonConvert.SerializeObject(encryInfo));
            Console.WriteLine(identity.encryptInfo);

            identity.fixBuyer = "T";
            identity.minage = "18";
            identity.type = "01";

            appenddata.identity = identity;
            appenddata.dgtlEnvlp = EncryptionUtil.RsaEncrypt(AesKey, WeiPayConfig.PublicKey, true);

            request.appendData = appenddata;
            request.data = dataBean;

            var chkStr = WeiPayUtils.GetSignData<M_JsApi.Data>(dataBean);
            string sign = EncryptionUtil.RSASignJavaBouncyCastle(chkStr.ToString(), WeiPayConfig.PrivateKey, "SHA256withRSA");

            request.sign = sign;
            var result = HttpPost<M_JsApi>(WeiPayConfig.ApiUrlJsapi, request);

        }
    }
}
